package com.yaorange.web;


import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Customer;
import com.yaorange.entity.CustomerSource;
import com.yaorange.entity.Emp;
import com.yaorange.service.CustomerService;
import com.yaorange.service.CustomerSourceService;
import com.yaorange.service.EmpService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/business")
public class BusinessHandler {

    @Resource
    private CustomerService customerService;
    @Resource
    private EmpService empService;
    @Resource
    private CustomerSourceService customerSourceService;


    //业务部模块-新订单 未沟通客户列表-分页数据获取
    @GetMapping("/new_order/untalk/page/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> page(@PathVariable Integer pageNum,
                                   @PathVariable Integer pageSize, @PathVariable String account){
        //通过员工账户获取员工对象
        Emp emp = empService.getEmpByUserName(account);
        //数据校验
        if(emp != null){
            //调用客户分页方法
            return customerService.getNewOrderList(pageNum,pageSize,emp.getEmpId());
        }
        return null;
    }

    //业务部模块-新订单 已沟通客户列表-分页数据获取
    @GetMapping("/new_order/told/page/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> followPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String account){
        //通过账户获取员工
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            return null;
        }
        return customerService.getFollowPage(pageNum,pageSize,emp.getEmpId());
    }



    //通过customerId查客户
    @GetMapping("/new_order/{customerId}")
    public Customer getCustomerById(@PathVariable Integer customerId){
        return customerService.getCustomerById(customerId);
    }


    //获取“客户来源”的下拉框数据
    @GetMapping("/new_order/sources")
    public List<CustomerSource> getCustomerSources(){
        //List<CustomerSource> list = customerSourceService.getList();
        //System.out.println(list);
        return customerSourceService.getList();
    }


    //"编辑"弹窗中的确定事件
    @PutMapping("/new_order/updateCustomer")
    public Map<String,Object> update(@RequestBody Customer customer){

        //设置追踪数据
        customer.setState(3);
        customer.setFllowDate(new Date());
        customer.setIsFollow(0);
        customer.setCallCount(1);

        int rows = customerService.updateCustomer(customer);
        //准备返回消息对象
        Map<String,Object> map = new HashMap<>();
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","编辑成功");
        }else{
            map.put("status",30000);
            map.put("msg","编辑失败");
        }
        return map;
    }


    //无效
    @PutMapping("/new_order/invalid")
    public Map<String,Object> invalid(@RequestBody Customer customer){
        //设置追踪数据
        customer.setState(8);//申请审核无效
        customer.setCallCount(1);
        int rows = customerService.update(customer);
        //准备返回消息对象
        Map<String,Object> map = new HashMap<>();
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","申请成功");
        }else{
            map.put("status",30000);
            map.put("msg","申请失败");
        }
        return map;
    }



    //有效更新
    @PutMapping("/valid/{customerId}")
    public Map<String,Object> valid(@PathVariable Integer customerId){
        Map<String, Object> map = new HashMap<>();

        int rows = customerService.updateById(customerId);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","操作成功");
        }else{
            map.put("msg","操作失败");
        }
        return map;
    }



    //下载顾客信息打印到表格中

}
